package com.example.practice.common;


import org.apache.commons.lang3.StringUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * TODO
 *
 * @author zgq
 * @date 2020/10/14 11:42
 */
public class DateUtil {

    public static void betweenDate(LocalDateTime start, LocalDateTime end) {
        long sec = ChronoUnit.SECONDS.between(start, end);
        long l1 = sec / 3600;
        long l = sec % 3600;
        long l2 = l / 60;
        long l3 = l % 60;
        System.out.println("间隔：" + l1 + "小时" + l2 + "分钟" + l3 + "秒");
    }

    public static LocalDateTime formatString(String dateString) {
        if (StringUtils.isBlank(dateString)) {
            return null;
        }
        return LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static void main(String[] args) {
        String start = "2020-10-10 11:00:00";
        String end = "2020-10-10 14:25:20";
//        String end   = "2020-10-10 12:01:20";
        betweenDate2(formatString(start), formatString(end));
    }

    /**
     * 方法2：计算两日期相差 几小时几分钟几秒
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return void
     * @author leining
     * @date 2020/10/14 13:36
     */
    public static void betweenDate2(LocalDateTime start, LocalDateTime end) {
        Duration between = Duration.between(start, end);
        long l1 = between.toHours();
        long l2 = between.toMinutes() % 60;
        long seconds = Duration.between(start, end).getSeconds();
        long l3 = seconds % 3600 / 60;
        System.out.println("间隔：" + l1 + "小时" + l2 + "分钟" + l3 + "秒");
    }
}
